package cn.yyh.user.controller;

import cn.yyh.basic.utils.ContextUtil;
import cn.yyh.basic.utils.PageInfo;
import cn.yyh.basic.utils.R;
import cn.yyh.user.pojo.po.UserAddress;
import cn.yyh.user.query.UserAddressQuery;
import cn.yyh.user.service.IUserAddressService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: UserAddressController
 * @Package: cn.yyh.user.controller
 * @date 2021/7/8
 */
@RestController
@RequestMapping("/userAddress")
public class UserAddressController {
    @Autowired
    private IUserAddressService userAddressService;

    @ApiOperation(value = "高级查询,并分页")
    @PostMapping("/pageQuery/{current}/{size}")
    public R pageQuery(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable Integer current,
            @ApiParam(name = "size",value = "每页展示条数",required = true)
            @PathVariable Integer size,
            @ApiParam(name = "query",value = "高级查询")
            @RequestBody(required = false) UserAddressQuery query) {

        // 设置当前页和每页条数
        query.setCurrentPage(current);
        query.setPageSize(size);
        try {
            PageInfo<UserAddress> pageInfo = userAddressService.pageQuery(query);
            return R.oK().data("totals", pageInfo.getTotals()).data("rows",pageInfo.getRows());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询用户地址")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id",value = "用户地址编号",required = true)
            @PathVariable Long id) {

        try {
            UserAddress byId = userAddressService.getById(id);
            return R.oK().data("rows", byId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据有没有ID判断是添加还是修改用户地址")
    @PostMapping("save")
    public R saveOrUpdate(
            @ApiParam(name = "userAddress",value = "数据封装对象")
            @RequestBody UserAddress userAddress) {

        try {
            if (userAddress.getId() == null) {
                userAddressService.save(userAddress);
            }else {
                userAddressService.update(userAddress);
            }
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID删除用户地址")
    @DeleteMapping("{id}")
    public R delete(
            @ApiParam(name = "id",value = "用户地址编号",required = true)
            @PathVariable Long id){

        try {
            userAddressService.delete(id);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "根据id批量删除用户地址")
    @PatchMapping("/deleteBatch")
    public R deleteBatch(
            @ApiParam(name = "ids",value = "用户地址编号集合",required = true)
            @RequestBody List<Long> ids){

        try {
            userAddressService.deleteBatch(ids);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查询所有用户地址")
    @GetMapping
    public R listAll(HttpServletRequest request) {
        try {
            List<UserAddress> list = userAddressService.listAll(
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId());
            return R.oK().data("address", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
